package com.clw.template.admin.controller;


import com.clw.template.entity.SysStorage;
import com.clw.template.service.ISysStorageService;
import com.clw.template.storage.StorageService;
import com.clw.template.util.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文件存储表 前端控制器
 * </p>
 *
 * @author clw
 * @since 2019-11-13
 */
@Api(tags = "SysStorageController",description = "存储管理")
@RestController
@RequestMapping("/admin/storage")
@Slf4j
public class SysStorageController {
    @Autowired
    private ISysStorageService sysStorageService;
    @Autowired
    private StorageService storageService;

    @ApiOperation("上传图片接口")
    @PostMapping("/create")
    public Object create(@RequestParam("file") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String url = sysStorageService.store(file.getInputStream(), file.getSize(), file.getContentType(), originalFilename);
        Map<String, Object> data = new HashMap<>();
        data.put("url", url);
        return CommonResult.success(data);
    }

    @ApiOperation("上传多个图片接口")
    @PostMapping("/creates")
    public Object creates(@RequestParam("file") MultipartFile[] files) throws IOException {

        StringBuilder urls = new StringBuilder();
        for (MultipartFile file : files){
            String originalFilename = file.getOriginalFilename();
            urls.append(sysStorageService.store(file.getInputStream(), file.getSize(), file.getContentType(), originalFilename));
            urls.append(";");
        }
        Map<String, Object> data = new HashMap<>();
        if (urls.indexOf(";") >= 0){
            data.put("urls", urls.toString().substring(0,urls.lastIndexOf(";")));//去除最后一个;
        }else {
            data.put("urls", urls.toString().substring(0,urls.lastIndexOf(";")));//去除最后一个;
        }

        return CommonResult.success(data);
    }

    @ApiOperation("回显图片接口")
    @GetMapping("/fetch/{key:.+}")
    public ResponseEntity<Resource> fetch(@PathVariable String key) {
        SysStorage sysStorage = sysStorageService.findByKey(key);
        if (key == null) {
            ResponseEntity.notFound();
        }
        String type = sysStorage.getType();
        MediaType mediaType = MediaType.parseMediaType(type);

        Resource file = storageService.loadAsResource(sysStorage.getName());
        if (file == null) {
            ResponseEntity.notFound();
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setCacheControl("max-age=604800");
        return ResponseEntity.ok().headers(headers).contentType(mediaType).body(file);
    }
}
